package com.zhuiYun.project.api.handworkdiycollection.service.Impl;

import com.zhuiYun.project.api.handworkdiycollection.entity.HandworkDiyCollection;
import com.zhuiYun.project.api.handworkdiycollection.entity.dto.HandworkDiyCollectionDTO;
import com.zhuiYun.project.api.handworkdiycollection.mapper.HandworkDiyCollectionMapper;
import com.zhuiYun.project.api.handworkdiycollection.model.HandworkDiyCollectionModel;
import com.zhuiYun.project.api.handworkdiycollection.service.HandworkDiyCollectionService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName handworkDiyServiceImpl
 * @Description TODO
 * @Author yangze
 * @Date 2023/4/6 13:42
 **/
@Service
public class HandworkDiyCollectionServiceImpl implements HandworkDiyCollectionService {

    @Resource
    private HandworkDiyCollectionMapper handworkDiyCollectionMapper;

    /**
     * @param userId
     * @param diyCommodityName
     * @Author yangze
     * @Description todo 查询用户手工diy收藏信息
     * @Date 2023/4/6 13:54
     * @Return
     */

    @Override
    public CommonResult selectHandworkDiyCollectionList(Integer userId, String diyCommodityName) throws CommonException {
        //校验id是否为空
        if (ObjectUtils.isNotEmpty(userId)) {
            //不为空
            List<HandworkDiyCollectionDTO> handworkDiyCollectionDTOS = handworkDiyCollectionMapper.selectHandworkDiyCollectionList(userId, diyCommodityName);
            //判断是否查到数据
            if(ObjectUtils.isNotEmpty(handworkDiyCollectionDTOS)){
                //有数据
                return CommonResult.ok(handworkDiyCollectionDTOS);
            }else{
                //无数据
                return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
            }
        } else {
            //为空
            return CommonResult.error(EmErrorCode.OBJECT_NOT_NULL);
        }
    }

    /**
     * @param id
     * @Author yangze
     * @Description TODO diy商品取消收藏
     * @Date 2023/4/6 19:57
     * @Return CommonResult
     */

    @Override
    public CommonResult deleteById(Integer id) throws CommonException {
        if (id == null || id == 0) {
            return CommonResult.error(EmErrorCode.OBJECT_NOT_NULL);
        }
        return CommonResult.ok(handworkDiyCollectionMapper.deleteById(id));
    }

    /**
     * @param handworkDiyCollectionModel
     * @Author yangze
     * @Description TODO 用户收藏手工diy商品
     * @Date 2023/4/10 8:53
     * @Return CommonResult
     */
    @Override
    public CommonResult insertHandworkDiyCollection(HandworkDiyCollectionModel handworkDiyCollectionModel) {
        // 判断是否已经收藏过该商品
        HandworkDiyCollection collection = handworkDiyCollectionMapper.selectByUserIdAndHandworkDiyId(handworkDiyCollectionModel.getUserId(), handworkDiyCollectionModel.getHandworkDiyId());
        // 如果已存在相同的收藏记录，则将该记录更新 delflag改为0
        if(collection !=null) {
            return CommonResult.ok(handworkDiyCollectionMapper.updateByUserIdAndHandworkDiyId(collection.getUserId(), collection.getHandworkDiyId()));
        } else {
            collection=new HandworkDiyCollection();
            BeanUtils.copyProperties(handworkDiyCollectionModel,collection);
            // 如果不存在相同的收藏记录，则创建一个新的收藏记录并插入到数据库中
            collection.setCreateTime(new java.util.Date());
            collection.setUpdateTime(new java.util.Date());
            collection.setDelFlag(0);
            return CommonResult.ok(handworkDiyCollectionMapper.insertSelective(collection));
        }
    }

    /**
     * @param userId,diyCommodityId
     * @Author yangze
     * @Description TODO 查询是否收藏 0未收藏 1已收藏
     * @Date 2023/4/10 10:30
     * @Return commonResult
     */
    @Override
    public CommonResult selectIsCollection(Integer userId, Integer diyCommodityId) {
        //获取收藏记录
        HandworkDiyCollection collection = handworkDiyCollectionMapper.selectByUserIdAndHandworkDiyId(userId, diyCommodityId);
        //如果收藏记录为空 或者delFlag为1，则返回false
        if(collection == null){
            return CommonResult.ok(false);
        }else{
            if(collection.getDelFlag() == 1){
                return CommonResult.ok(false);
            }
            //如果收藏记录不为空 且delFlag为0，则返回true 代表有真实收藏记录
            return CommonResult.ok(true);
        }
    }

    /**
    * @Author yangze
    * @Description 根据用户id和diy商品id取消收藏
    * @Date 2023/4/10 14:55
    * @Return 
    * @param handworkDiyCollectionModel
    */
    @Override
    public CommonResult deleteByUserIdAndDiyId(HandworkDiyCollectionModel handworkDiyCollectionModel) {
        return CommonResult.ok(handworkDiyCollectionMapper.deleteByUserIdAndDiyId(handworkDiyCollectionModel.getUserId(), handworkDiyCollectionModel.getHandworkDiyId()));
    }

}
